package com.system.controller;

import com.system.pojo.Administrator;
import com.system.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * Created by 陈文杰 on 2022/6/8 21:32
 */
@Controller
public class UserController {
    @Autowired
    private AdministratorService administratorService;
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session)  {
        Administrator administrator = administratorService.queryAdministratorByName(username);
        System.out.println(username);
        if (administrator==null){
            session.setAttribute("inexistence","no");//不存在此管理员
            return "redirect:loginfail";
        }
        if(administrator.getAdminPassword().equals(password)){
                session.setAttribute("user",administrator);
                return "redirect:toWelcome";
        }
        return "redirect:index.jsp";
    }



    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(String username,String password){
        if (administratorService.queryAdministratorByName(username)==null){
            Administrator administrator = new Administrator();
            administrator.setAdminName(username);
            administrator.setAdminPassword(password);
            administratorService.addAdministrator(administrator);
            return "logincome";
        }
        return "404";
    }

    @RequestMapping("/toMindex")
    public String toMindex(HttpSession session){
        if (session.getAttribute("user")!=null){
            return "mindex";
        }
        return "redirect:index.jsp";
    }

    @RequestMapping("/toWelcome")
    public String toWelcome(){
        return "welcome";
    }

    @RequestMapping("/returnMindex")
    public String returnMindex(){
        return "returnMindex";
    }
}
